package com.xinchuang.utils;

import com.xinchuang.util.LngLatUtil;
import org.locationtech.jts.geom.Geometry;
import org.locationtech.jts.geom.GeometryFactory;
import org.locationtech.jts.geom.Polygon;
import org.locationtech.jts.io.ParseException;
import org.locationtech.jts.io.geojson.GeoJsonReader;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

/**
 * TODO
 *
 * @author zkwczx
 * @version 1.0
 * @date 2024/2/19 16:07
 */
public class GeoUtil {

    public static void main(String[] args) throws Exception {
        // 假设你已经有了一个GeoJSON字符串代表矩形区域
        String geoJsonString = "{\"type\":\"Polygon\",\"coordinates\":[[            [\n" +
                "              100,\n" +
                "              20\n" +
                "            ],\n" +
                "            [\n" +
                "              101,\n" +
                "              20\n" +
                "            ],\n" +
                "            [\n" +
                "              101,\n" +
                "              21\n" +
                "            ],\n" +
                "            [\n" +
                "              100,\n" +
                "              21\n" +
                "            ],\n" +
                "            [\n" +
                "              100,\n" +
                "              20\n" +
                "            ]" +
                "]]}";



        ObjectMapper mapper = new ObjectMapper();
        JsonNode geoJsonNode = mapper.readTree(geoJsonString);

        // 使用GeoJsonReader将JsonNode转换为JTS的Geometry对象
        GeoJsonReader reader = new GeoJsonReader();
        Geometry geometry = reader.read(geoJsonString);

        // 检查geometry是否为Polygon类型
        if (geometry instanceof Polygon) {
            // 计算Polygon的面积
            Polygon polygon = (Polygon) geometry;
            double area = polygon.getArea();

            System.out.printf("The area of the GeoJSON rectangle is %.2f square units.\n", area);
        } else {
            System.out.println("Provided GeoJSON data does not represent a Polygon.");
        }

        // 解析矩形区域的顶点坐标（假设坐标已经是投影到平面后以米为单位）
        JsonNode coordinatesNode = geoJsonNode.get("coordinates").get(0);
        double[][] rectangleCorners = new double[coordinatesNode.size()][];
        for (int i = 0; i < coordinatesNode.size(); i++) {
            JsonNode pointNode = coordinatesNode.get(i);
            double[] pointCoordinates = new double[pointNode.size()];
            for (int j = 0; j < pointNode.size(); j++) {
                pointCoordinates[j] = pointNode.get(j).asDouble();
            }
            rectangleCorners[i] = pointCoordinates;
        }

        // 计算矩形区域的长和宽（单位为米）
        double rectWidth = Math.abs(rectangleCorners[1][0] - rectangleCorners[0][0]);
        System.out.println("rectWidth = " + rectWidth);
        double rectHeight = Math.abs(rectangleCorners[2][1] - rectangleCorners[0][1]);
        System.out.println("rectHeight = " + rectHeight);
        // 假设你要放置的矩形物体的尺寸（单位也为米）
        double objectWidth = 1.0; // 示例物体宽度为1米
        double objectHeight = 1.0; // 示例物体高度为1米

        // 计算可以放置的最大数量（理想情况下的理论值，无间隙且物体尺寸与区域对齐）
        int maxObjects = (int) Math.floor(rectWidth / objectWidth) * (int) Math.floor(rectHeight / objectHeight);

        System.out.println("理论上可以放置的最大矩形物体数量: " + maxObjects);

        System.out.println(LngLatUtil.getDistance(100, 20, 101, 20));

//        // 解析矩形区域的顶点坐标
//        JsonNode coordinatesNode = geoJsonNode.get("coordinates").get(0);
//        double[][] rectangleCorners = new double[coordinatesNode.size()][];
//        for (int i = 0; i < coordinatesNode.size(); i++) {
//            JsonNode pointNode = coordinatesNode.get(i);
//            double[] pointCoordinates = new double[pointNode.size()];
//            for (int j = 0; j < pointNode.size(); j++) {
//                pointCoordinates[j] = pointNode.get(j).asDouble();
//            }
//            rectangleCorners[i] = pointCoordinates;
//        }
//
//        // 计算矩形区域的长和宽
//        double rectWidth = Math.abs(rectangleCorners[1][0] - rectangleCorners[0][0]);
//        double rectHeight = Math.abs(rectangleCorners[2][1] - rectangleCorners[0][1]);
//
//        // 假设你要放置的矩形物体的尺寸
//        double objectWidth = 0.1;
//        double objectHeight = 0.1;
//
//        // 计算可以放置的最大数量（理想情况下的理论值，无间隙且物体尺寸与区域对齐）
//        int maxObjects = (int) Math.floor(rectWidth / objectWidth) * (int) Math.floor(rectHeight / objectHeight);
//
//        System.out.println("理论上可以放置的最大矩形物体数量: " + maxObjects);
    }

}
